<template>
  <el-dialog
    :model-value="editVisible"
    title="编辑用户信息"
    width="60%"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" label-width="90px" :rules="rules">
      <el-form-item label="满分" prop="score">
        <el-input v-model="form.score" />
      </el-form-item>
      <el-form-item label="试卷名" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="建议时间" prop="suggestTime">
        <el-input v-model="form.suggestTime" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch } from 'vue'
import { editpaper } from '@/api/paper'
import { ElMessage } from 'element-plus'
const emits = defineEmits(['update:modelValue', 'inituserlist'])
const formRef = ref(null)
const form = ref({
  createTime: '2022-02',
  createUser: 2,
  deleted: false,
  frameTextContentId: 52,
  gradeLevel: 1,
  id: 2,
  limitEndTime: null,
  limitStartTime: null,
  name: 'C语言编程题练习（进阶）',
  paperType: 1,
  questionCount: 6,
  score: 600,
  subjectId: 1,
  suggestTime: 60,
  taskExamId: null
})

//  接收传过来的原信息
const props = defineProps({
  rowvalue: {
    type: Object,
    default: () => {}
  }
})

// 监听原信息是否改变
watch(
  () => props.rowvalue,
  () => {
    console.log(props.rowvalue)
    form.value = props.rowvalue
  },
  { deep: true, immediate: true }
)

const handleClose = async () => {
  emits('update:modelValue', false)
}

const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      ElMessage({
        message: '试卷信息修改成功',
        type: 'success'
      })
      await editpaper(form.value)
      emits('inituserlist')
    } else {
      ElMessage({
        message: '试卷信息修改失败',
        type: 'warning'
      })
      return false
    }
  })

  handleClose()
}
const rules = ref({
  username: [
    { required: true, message: '请输入有效用户名', trigger: 'blur' },
    { min: 3, max: 15, message: 'Length should be 3 to 5', trigger: 'blur' }
  ],
  password: [
    {
      required: true,
      message: '请输入有效密码',
      trigger: 'change'
    }
  ]
})
</script>

<style lang="scss" scoped></style>
